package cn.whuc.sparksql

import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, SparkSession}

import java.util.Properties

object Demo_load_mysql {
  def main(args: Array[String]): Unit = {
    val ss: SparkSession = SparkSession
      .builder()
      .config(new SparkConf()
        .setMaster("local[*]")
        .setAppName(" "))
      .getOrCreate()

    val p: Properties = new Properties() // 创建p对象 存放字符串型kv

    p.setProperty("user","root")
    p.setProperty("password","111111")

    val df1: DataFrame = ss.read.jdbc("jdbc:mysql://localhost:3306/data1", "emp", p)

    df1.show()

    // 第二种
    val df2: DataFrame = ss
      .read
      .format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/data1")
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root").option("password", "111111")
      .option("dbtable", "emp")
      .load()

    df2.show()
  }
}
